package com.bike.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhPayConfig;
import com.bike.mapper.HhPayConfigMapper;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.bike.util.TransFerUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class HhPayConfigServiceImpl extends BaseServcieImpl  {

	@Autowired
	HhPayConfigMapper hhPayConfigMapper;
	
	/**
	* 保存
	*/
	public void saveOrUpdate(PageData pd, HhPayConfig record) {
		HhPayConfig  hhPayConfig=hhPayConfigMapper.selectByPrimaryKey(record.getBusinessId());
		if(hhPayConfig!=null){
			 //编辑
			hhPayConfigMapper.updateByPrimaryKeySelective(record);
		}else{
			record.setAddTime(TimeManager.currentTime());
			hhPayConfigMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteHhPayConfigById(long payConfigId) {
		return hhPayConfigMapper.deleteByPrimaryKey(payConfigId);
	}
	
	/**
	*列表分页
	*/
	public PageData selectHhPayConfigList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
		String advanceFilter=pd.getString("advanceFilter");
		if(advanceFilter!=null&&!advanceFilter.equals("")){
			advanceFilter=advanceFilter.replaceAll("'", "");
			JSONArray array=JSONObject.parseArray(advanceFilter);
			StringBuffer buffer=new StringBuffer();
			for(int i=0;i<array.size();i++){
				JSONObject object=array.getJSONObject(i);
				String field=object.getString("field");
				String op=object.getString("op");//equal  contains  notequal  greater  greaterorequal  less  lessorequal  beginwith  endwith
				String value=object.getString("value");
				buffer.append(" and ").append(TransFerUtil.getMybatisField(field)).append(TransFerUtil.getMybatisOperation(op,value));
			}
			if(array.size()>0){
				pd.put("condition", buffer.toString());
			}
		}
		List<HhPayConfig> records= hhPayConfigMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<HhPayConfig>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<HhPayConfig> listAll(PageData pd) {
		return  hhPayConfigMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public HhPayConfig selectByHhPayConfigId(Long payConfigId) {
		return hhPayConfigMapper.selectByPrimaryKey(payConfigId);
	}
}

